#include "experiments.h"

#include "../../runtime/AutomaticBoxCreatorRegistration.h"

#include <iostream>

namespace jm4bbx {
	REGISTER_BOX_TO_RUNTIME_BASE(b1, B1)
	REGISTER_BOX_TO_RUNTIME_BASE(b2, B2)

#define DATA_COUNT 10

	void B1::initImpl() {
		add_group(groupType(0), columnType(0), get_input_descriptors(inType(0)));
		add_group(groupType(1), columnType(0), get_output_descriptors(outType(0)));
	
		receive(inType(0),groupType(0));
		getGroupPtr(groupType(1))->allocate(DATA_COUNT);
	}

	void B1::syncMachEtwas() {

		for (int i = 0; i < DATA_COUNT; ++i) {
			get_data<IntegerKeyRecordPolicyType>(columnType(0))[i] = IntegerKeyRecord(i, -i);
		}		

		GroupPtr g = getGroupPtr(groupType(1));

		g->relocate(0, DATA_COUNT);
		send(outType(0), groupType(1));

		//g->relocate(5, 7);
		//send(outType(0), groupType(1));

		receive(inType(0),groupType(0));
	}

	B1::B1(const BoxParamPack& p) : RootBox(p) {

	}


	void B2::initImpl() {
		add_group(groupType(0), columnType(0), get_input_descriptors(inType(0)));
		add_group(groupType(1), columnType(0), get_output_descriptors(outType(0)));

		receive(inType(0),groupType(0));
	}

	void B2::syncMachEtwas() {

		GroupPtr g = getGroupPtr(groupType(0));
		for (;g->getReadPosition() < g->getReadEnd(); g->moveReadPosition()) {

			std::cout << get_data<IntegerKeyRecordPolicyType>(columnType(0))[g->getReadPosition()] << std::endl;

		}		
		receive(inType(0), groupType(0));
		//send_poisoned(outType(0));
	}

} // namespace jm4bbx
